package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.flag.ResourcesPath;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	import newx.com.loader.SwfLoader;
	
	public class SpecialKungfuInfoLogic
	{
		private static var _self:SpecialKungfuInfoLogic;
		private var _pointfalg:int =-1;
		private var _bindFlag:int = -1;
		private var _skillflag:int = -1;
		private var _ui	: SpecialKungfuInfoUI;
		private var _bindManang:BindManage;
		private var _model:ModelLocator;
		private var _currSkill:SkillStree;
		private var _needExp:int;
		private var _currExp:int;
		private var _needItem:int;
		
		/**
		 *是否需要引导 
		 */		
		private var _isNeedChannel:Boolean;
		
		/**
		 * 引导阶段  -1不需要引导   1 开始点击升级引导  2 开始点击升级引导 
		 */		
		private var _step:int =-1;
		public static function getInstance():SpecialKungfuInfoLogic
		{
			if(_self == null)
			{
				_self = new SpecialKungfuInfoLogic();
			}
			return _self;
		}
		public function SpecialKungfuInfoLogic()
		{
			
		}
		public function  Init():void
		{
			_bindManang= BindManage.getInstance();
			_model = ModelLocator.getInstance();
			_ui = SpecialKungfuInfoUI.getInstance();
			_ui.nextnamelabel.glowColor =_ui.namelabel.glowColor =_ui.nextLabel.glowColor =_ui.infolabel.glowColor =0xf6f1e1;
			_ui.channelMc1.mouseChildren = _ui.channelMc1.mouseEnabled =_ui.channelMc2.mouseChildren = _ui.channelMc2.mouseEnabled = false;
		}
		public function	BeforeShow():void
		{
			addEvent();
			bind();
			updateInfo();
		}
		public function	AfterHide():void
		{
			removeEvent();
			unbind();
			if(!SpecialKungFuUI.getInstance().created)
			{
				SpecialKungFuLogic.getInstance().unbind();
			}
		}
		private function bind():void
		{
			if(_pointfalg == -1)
			{
				_pointfalg = _bindManang.BindProp(_model.scene.Role,"SkillExp",this,"skillPonit");
			}
			if(_skillflag == -1)
			{
				_skillflag = _bindManang.BindProp(_model.scene.Role,"DefaultUltraSkill",this,"DefaultSkill");
			}
			if( _bindFlag == -1 )
			{
				_bindFlag		=	_bindManang.BindView(_model.scene,View.VIEWPORT_TOOL,OnAddPack,OnRemovePack,null,updatafunc);
				
			}
		}
		private function unbind():void
		{
			if(_pointfalg != -1)
			{
				_pointfalg = _bindManang.UnbindProp(_model.scene.Role,"SkillExp",_pointfalg);
			}
			if( _bindFlag != -1 )
			{
				_bindFlag		=	_bindManang.UnbindView(View.VIEWPORT_TOOL,_bindFlag);
			}
			if(_skillflag != -1)
			{
				_skillflag = _bindManang.UnbindProp(_model.scene.Role,"DefaultUltraSkill",_skillflag);
			}
		}
		public function set DefaultSkill(val:int):void
		{
			_ui.useBtn.enabled 			= _currSkill.curr &&( _currSkill.curr !=val)?true:false;
			isChannel();
		}
		private function isChannel():void
		{
			if(_model.scene.Role.GetProperties("DefaultUltraSkill") == 81010430)
			{
				_isNeedChannel = true;
			}
			else
			{
				_isNeedChannel = false;
			}
			if(!_isNeedChannel)
			{
				_step =-1;

			}
			else if(_ui.upgradeBtn.enabled)
			{
				_step = 1;
			}
			else if(_ui.useBtn.enabled)
			{
				_step = 2;
			}
			showChannel();
		}
		private function showChannel():void
		{
			
			switch(_step)
			{
				case -1:
					_ui.channelMc1.visible  =false;
					_ui.channelMc2.visible =false;
					_ui.channelMc1.stop();
					_ui.channelMc2.stop();
					break;
				case 1:
					_ui.channelMc1.visible =true;
					_ui.channelMc2.visible =false;
					_ui.channelMc1.play();
					break;
				case 2:
					_ui.channelMc2.visible =true;
					_ui.channelMc1.visible =false;
					_ui.channelMc2.play();
					break;
			}
		}
		public function get needItem():int
		{
			return _needItem;
		}
		public function updateBindInfo(PropertiesValue:ViewObject):void
		{
			if(PropertiesValue.GetProperties("ConfigID") == _needItem)
			{
				updateInfo();
			}
		}
		private function OnAddPack(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			if(PropertiesValue.GetProperties("ConfigID") == _needItem)
			{
				updateInfo();
			}
		}
		private function updatafunc(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			if(PropertiesValue.GetProperties("ConfigID") == _needItem)
			{
				updateInfo();
			}
		}
		private function OnRemovePack(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			if(PropertiesValue.GetProperties("ConfigID") == _needItem)
			{
				updateInfo();
			}
		}
		public function set skillPonit(val:int):void
		{
			_currExp = val;
			updateInfo();
		}
		private function addEvent():void
		{
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onColse);
			_ui.useBtn.addEventListener(MouseEvent.CLICK,onClickHandle);
			_ui.upgradeBtn.addEventListener(MouseEvent.CLICK,onClickHandle);
		}
		private function removeEvent():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onColse);
			_ui.useBtn.removeEventListener(MouseEvent.CLICK,onClickHandle);
			_ui.upgradeBtn.removeEventListener(MouseEvent.CLICK,onClickHandle);
		}
		public function set currSkill(s:SkillStree):void
		{
			_currSkill = s;
			updateInfo();
			if(!_ui.created)
			{
				_ui.showOrHide();
			}
		}
		public function onLoadUrl():void
		{
			const model:String = "kungfuBg1"
			var url:String = ResourcesPath.getSpecialKungFuModel(model)
			SwfLoader.StartLoad(url,null,completeFunc);
		}
		private function completeFunc(val:DisplayObject,plist:Array ):void
		{
			if(val)
			{
				_ui.img.source = val ; 
				_ui.showUI();
			}
		
		}
		public function get currSkill():SkillStree
		{
			return _currSkill;
		}
		public function updateInfo():void
		{
			var name:String;
			var info:String;
			var nextinfo:String;
			if(_currSkill.curr ==0)  //尚未学习
			{
				name = StaticIni.getSwfLangStr("NationString_000");
				info = StaticIni.getSwfLangStr("SpecialKungfuInfoUIStr_2");
				_ui.infolabel.htmlText = info;;
			}
			else
			{
				name = StaticIni.getSwfLangStr(_currSkill.curr+"");
				_ui.infolabel.htmlText = StaticIni.getSwfLangStrVar("SpecialKungfuInfoUIStr_6",[StaticIni.getSwfLangStr("SkillDes_"+_currSkill.curr)]) ;
			}
			_ui.namelabel.htmlText 	=StaticIni.getSwfLangStrVar("SpecialKungfuInfoUIStr_4",[name]) ;
			_needExp 				= int(StaticIni.getIniVar(_currSkill.next,"NeedExp"));
			var per:Number 			= 0 ;
			if(_needExp)
			{
				per = _currExp/_needExp;
				_ui.explabel.htmlText = StaticIni.getSwfLangStrVar("SpecialKungfuInfoUIStr_7",[StaticIni.getSwfLangStr("33990001"),_currExp,_needExp]);
			}
			else
			{
				 _needItem	= int(StaticIni.getIniVar(_currSkill.next,"NeedItem"));
				var haveItemNum:int = ComposeItemLogic.getInstance().searchItem(_needItem,1);
				var needItemNum:int = int(StaticIni.getIniVar(_currSkill.next,"NeedItemNum"));
				per =haveItemNum/needItemNum>1?100:haveItemNum/needItemNum;
				if(_needItem==0)
				{
					_ui.explabel.htmlText ="";
				}
				else
				{
					_ui.explabel.htmlText =StaticIni.getSwfLangStrVar("SpecialKungfuInfoUIStr_7",[StaticIni.getSwfLangStr(_needItem+""),haveItemNum,needItemNum]);
				}
			}
			_ui.expbar.bar 				= per*100;
			_ui.useBtn.enabled 			= _currSkill.curr &&( _currSkill.curr != _model.scene.Role.GetProperties("DefaultUltraSkill"))?true:false;
			if(_currSkill.next ==0) //最高级
			{
				_ui.nextnamelabel.htmlText 	= ""//StaticIni.getSwfLangStrVar("SpecialKungfuInfoUIStr_5",[StaticIni.getSwfLangStr("NationString_000")]);
				_ui.nextLabel.htmlText 		= StaticIni.getSwfLangStr("SpecialKungfuInfoUIStr_3");
				_ui.upgradeBtn.enabled = false;
			}
			else
			{
				_ui.upgradeBtn.enabled 		= per<1?false:true;
				_ui.nextnamelabel.htmlText 	= StaticIni.getSwfLangStrVar("SpecialKungfuInfoUIStr_5",[StaticIni.getSwfLangStr(_currSkill.next+"")]);
				_ui.nextLabel.htmlText 		= StaticIni.getSwfLangStrVar("SpecialKungfuInfoUIStr_6",[StaticIni.getSwfLangStr("SkillDes_"+_currSkill.next)]);
			}
			isChannel();
			
		}
		private function onColse(e:NxWindowEvent):void
		{
			_ui.hide();
		}
		private function onClickHandle(e:MouseEvent):void
		{
			switch(e.currentTarget)
			{
				case _ui.useBtn:
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SET_DEFAULT_SKILL,_currSkill.curr);
					if(_step!= -1)
					{
						_step = -1;
					}
					break;
				case _ui.upgradeBtn:
					SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_SKILL_UPGRADE,_currSkill.next);
					if(_step == 1)
					{
						_step = 2;
					}
					break;
			}
			showChannel();
		}
	}
}